<?php include('actions/user_auth.php'); ?>
<?php include('includes/start.php'); ?>

<?php
$con = mysql_connect($dbserver, $dbuser, $dbpass);
if (!$con)
{
	echo "<p>Connection to server failed</p>";
}
else
{
	mysql_select_db($db, $con);
	echo "<h2>" . $_SESSION['name'] . "</h2>";
	if ($_SESSION['admin'])
	{
		echo "<h3>Stats</h3>";
		$q = fquery($books_checked_out, array($_SESSION['id']));
		$result = mysql_fetch_array(mysql_query($q));
		echo "<b>Books Checked Out:</b> ";
		if ($result['count']) echo $result['count']; else echo "0";
		echo "<br/>";
		$q = fquery($books_checked_in, array($_SESSION['id']));
		$result = mysql_fetch_array(mysql_query($q));
		echo "<b>Books Checked In:</b> ";
		if ($result['count']) echo $result['count']; else echo "0";
		echo "<br/>";	
		$q = fquery($patrons_signed_up, array($_SESSION['id']));
		$result = mysql_fetch_array(mysql_query($q));
		echo "<b>Patrons Signed Up: </b> ";
		if ($result['count']) echo $result['count']; else echo "0";
	}
	else
	{
		echo "<h3>Personal Information</h3>";
		$q = fquery($patron_information, array($_SESSION['id']));
		$result = mysql_fetch_array(mysql_query($q));
		echo "<b>Birthday: </b>" . $result['birth_date'] . "<br/>";
		echo "<b>E-mail:</b> " . $result['email'] . "<br/>";
		echo "<b>Phone Number:</b> " . $result['phone_number'] . "<br/>";
		echo "<b>Address:</b> " . $result['street'] . ", " . $result['city'] . ", " . $result['province'] . ", " . $result['postal_code'] . "<br/>";
		echo "<b>Dues: </b>$" . number_format(((double)$result['dues'])/((double)100), 2) . "<br/><br/>";

		
		echo "<h3>Books Checked Out</h3>";
		if (isset($_GET['err'])) echo "<div id='err'>" . $_GET['err'] . "</div>";
		$q = fquery($user_checked_out_query, array($_SESSION['id']));
		$result = mysql_query($q);
		if (mysql_num_rows($result) == 0)
		{
			echo "<p>You have no books checked out</p>";
		}
		else
		{
			echo "<table><tr><th>Renew</th><th>Book Title</th><th>Type</th><th>Renewal Count</th><th>Renewal Limit</th><th>Due Date</th></tr>";
			while ($row = mysql_fetch_array($result))
			{
				echo "<tr><td><a href='actions/renew.php?id=" . $row['inventory_id'] . "'>RENEW</a></td><td>" . $row['title'] . "</td><td>" . $row['name'] . "</td><td>" . $row['renewal_count'] . "</td><td>" . $row['renewal_limit'] . "</td><td>" . date('Y-m-d', strtotime('+' . ($row['checkout_duration']*($row['renewal_count']+1)) . ' day', strtotime($row['date_checked_out']))) . "</td></tr>";
			}
			echo "</table>";
		}
	}
	mysql_close($con);
}
?>

<?php include('includes/stop.php'); ?>
